package memory.net;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import memory.*;


@SuppressWarnings("serial")
public class ClientLobby extends JFrame implements ActionListener {
	
	private MainWindow.mode mode;
	private ClientThread cT;
	private ConnectClients connector;
	private GameControl gC;
	
	
	//Lobby
	private JPanel lobbyPanel = new JPanel();
	private JLabel lobbyLabel = new JLabel("Lobby");
	private JTextArea chatArea = new JTextArea();
	private JScrollPane chatAreaPanel = new JScrollPane(chatArea);
	private DefaultListModel playersListModel = new DefaultListModel();
	private JList playersList = new JList(playersListModel);
	private JScrollPane playersListPanel = new JScrollPane(playersList);
	private JTextField chatEntryField = new JTextField(25);
	
	//ButtonsPanel
	private JPanel buttonsPanel = new JPanel();
	private JButton leaveGameButton = new JButton("Leave game");
	private JButton startGameButton = new JButton("Start game");
	private JButton stopServerButton = new JButton("Stop server");
	
	private MainWindow mwRef;
	
	//Build window method
	private void buildWindow(JFrame pw,MainWindow.mode m) {
		
		setLocationRelativeTo(mwRef);
		setIconImage(Toolkit.getDefaultToolkit().getImage("./data/1.png"));
		setResizable(false);
		
		lobbyLabel.setFont(new Font("Sans Serif",1,18));
		lobbyPanel.setLayout(new GridBagLayout());
		buttonsPanel.setLayout(new FlowLayout());
		
		playersListPanel.setPreferredSize(new Dimension(100,200));
		chatArea.setEditable(false);
		chatAreaPanel.setPreferredSize(new Dimension(276,197));
		
		GridBagConstraints lobbyConstraints = new GridBagConstraints();
		
		//Add lobby
		add(lobbyPanel,BorderLayout.CENTER);
		
		lobbyConstraints.insets = new Insets(10,2,10,10);
		
		lobbyConstraints.gridx = 0;
		lobbyConstraints.gridy = 0;
		lobbyConstraints.gridwidth = 2;
		lobbyPanel.add(lobbyLabel,lobbyConstraints);
		
		lobbyConstraints.insets = new Insets(0,2,0,10);
		
		lobbyConstraints.gridx = 0;
		lobbyConstraints.gridy = 1;
		lobbyConstraints.gridwidth = 1;
		lobbyPanel.add(playersListPanel,lobbyConstraints);
		
		lobbyConstraints.gridx = 1;
		lobbyConstraints.gridy = 1;
		lobbyConstraints.gridwidth = 1;
		lobbyPanel.add(chatAreaPanel,lobbyConstraints);
		
		lobbyConstraints.gridx = 1;
		lobbyConstraints.gridy = 2;
		lobbyConstraints.gridwidth = 1;
		lobbyPanel.add(chatEntryField,lobbyConstraints);
			
		//Add buttons
		add(buttonsPanel,BorderLayout.SOUTH);
		buttonsPanel.add(leaveGameButton);
		buttonsPanel.add(startGameButton);
		buttonsPanel.add(stopServerButton);
				
		//Add listener
		leaveGameButton.addActionListener(this);
		startGameButton.addActionListener(this);
		stopServerButton.addActionListener(this);
		chatEntryField.addActionListener(this);
		
		if (m == MainWindow.mode.MPS) {
			leaveGameButton.setEnabled(false);
		}
		else if (m == MainWindow.mode.MPC) {
			startGameButton.setEnabled(false);
			stopServerButton.setEnabled(false);
		}
		
		//Junk
		pack();
		setVisible(true);
	}

	
	
	
	
	//Server constructor
	public ClientLobby(JFrame pw , ConnectClients cc , String name) {
		
		mode = MainWindow.mode.MPS;
		
		gC = cc.getGameController();
		
		ServerWindow herde = (ServerWindow)pw;
		mwRef = herde.mwRef;
		herde.dispose();
		
		buildWindow(mwRef,MainWindow.mode.MPS);
		connector = cc;
		
		try {
			Socket socketen = new Socket("localhost",Options.getPort());
			socketen.setSoTimeout(500);
			Player playern = new Player(name);
			NetEventListener listenern = new NetEventListener(this);
			cT = new ClientThread(playern,socketen,listenern,new DebugWindow());
			listenern.setClientThread(cT);
		}
		catch (Exception e) {}
	}
	
	
	
	
	//Client constructor
	public ClientLobby(JFrame pw , String ip, int p , String name) {
		
		mode = MainWindow.mode.MPC;
		
		ClientWindow herde = (ClientWindow)pw;
		mwRef = herde.mwRef;
		herde.dispose();
		
		buildWindow(mwRef,MainWindow.mode.MPC);
		
		try {
			Socket socketen = new Socket(ip,p);
			socketen.setSoTimeout(500);
			Player playern = new Player(name);
			NetEventListener listenern = new NetEventListener(this);
			cT = new ClientThread(playern,socketen,listenern,new DebugWindow());
			listenern.setClientThread(cT);
		} catch (Exception e) {}
	}
	
	public void createMainWindow() {
		
		Point p = mwRef.getLocation();
		
		if (mode == MainWindow.mode.MPS) {
			new MainWindow(p, gC,cT);
		}
		
		else if (mode == MainWindow.mode.MPC) {
			new MainWindow(p,cT);
		}
		mwRef.dispose();
		
	}
	
	
	
	// TODO ip-check, enabling player name collisions
	//Instance methods
	public void updatePlayerList(String s) {
		if (!playersListModel.contains(s)) {
			playersListModel.addElement(s);
		}
	}
	
	
	// TODO ip-recognision
	public void deletePlayer(String s) {	
		playersListModel.removeElement(s);
	}
	
	
	//Send message!
	private void sendChatMsg(String s) {
		cT.addToQueue(new WrapperClass("New Chat Message",s));
	}
	
	//Recieve message
	public void addChatMsg(String s) {
		chatArea.append(s + "\n");
	}
	
	
	//Actions!
	public void actionPerformed(ActionEvent e) {
		
		//Chat field
		if (e.getSource() == chatEntryField) {
			String msg = chatEntryField.getText();
			if (msg.length() > 0) {
				//chatArea.append("> " + msg + "\n");
				chatEntryField.setText("");
				sendChatMsg(msg);
			}
		}

		
		
		//Start game button
		else if (e.getSource() == startGameButton) {
			
			connector.startGame();
		//	createMainWindow();
		
			
		}
		
		else if (e.getSource() == stopServerButton) {	
			dispose();			
		}
		
		//Leave button
		else if (e.getSource() == leaveGameButton) {
			cT.leaveGame();
			dispose();
		}
		
	}

	
	
	
	
}



